package com.kaesun.ufit.tools;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.kaesun.ufit.weight.R;

public class CircleView extends View {

	private static final int DEF_WIDTH = 420;
	private static final int DEF_HEIGHT = 400;

	public static final int SLIDER_OUT_START = 135;
	public static final int SLIDER_OUT_RANGE = 270;

	public static final int SLIDER_IN_START = 150;
	public static final int SLIDER_IN_RANGE = 240;

	public static final int HANDLER_PARENT = 2;

	private static final int IMAGE_CIRCLE[] = {
		R.drawable.img_circle_01, R.drawable.img_circle_02, R.drawable.img_circle_03,
		R.drawable.img_circle_04, R.drawable.img_circle_05, R.drawable.img_circle_06,
		R.drawable.img_circle_07, R.drawable.img_circle_08, R.drawable.img_circle_09,
		R.drawable.img_circle_10,
		R.drawable.img_circle_11, R.drawable.img_circle_12, R.drawable.img_circle_13,
		R.drawable.img_circle_14, R.drawable.img_circle_15, R.drawable.img_circle_16,
		R.drawable.img_circle_17, R.drawable.img_circle_18, R.drawable.img_circle_19,
		R.drawable.img_circle_20,
		R.drawable.img_circle_21, R.drawable.img_circle_22, R.drawable.img_circle_23,
		R.drawable.img_circle_24, R.drawable.img_circle_25, R.drawable.img_circle_26,
		R.drawable.img_circle_27, R.drawable.img_circle_28, R.drawable.img_circle_29,
		R.drawable.img_circle_30,
		R.drawable.img_circle_31, R.drawable.img_circle_32, R.drawable.img_circle_33,
		R.drawable.img_circle_34, R.drawable.img_circle_35, R.drawable.img_circle_36,
		R.drawable.img_circle_37, R.drawable.img_circle_38, R.drawable.img_circle_39,
		R.drawable.img_circle_40,
		R.drawable.img_circle_41, R.drawable.img_circle_42, R.drawable.img_circle_43,
		R.drawable.img_circle_44, R.drawable.img_circle_45, R.drawable.img_circle_46,
		R.drawable.img_circle_47, R.drawable.img_circle_48, R.drawable.img_circle_49,

		R.drawable.img_circle_48, R.drawable.img_circle_47, R.drawable.img_circle_46,
		R.drawable.img_circle_45, R.drawable.img_circle_44, R.drawable.img_circle_43,
		R.drawable.img_circle_42, R.drawable.img_circle_41, R.drawable.img_circle_40,
		R.drawable.img_circle_39, R.drawable.img_circle_38, R.drawable.img_circle_37,
		R.drawable.img_circle_36, R.drawable.img_circle_35, R.drawable.img_circle_34,
		R.drawable.img_circle_33, R.drawable.img_circle_32, R.drawable.img_circle_31,
		R.drawable.img_circle_30,
		R.drawable.img_circle_29, R.drawable.img_circle_28, R.drawable.img_circle_27,
		R.drawable.img_circle_26, R.drawable.img_circle_25, R.drawable.img_circle_24,
		R.drawable.img_circle_23, R.drawable.img_circle_22, R.drawable.img_circle_21,
		R.drawable.img_circle_20,
		R.drawable.img_circle_19, R.drawable.img_circle_18, R.drawable.img_circle_17,
		R.drawable.img_circle_16, R.drawable.img_circle_15, R.drawable.img_circle_14,
		R.drawable.img_circle_13, R.drawable.img_circle_12, R.drawable.img_circle_11,
		R.drawable.img_circle_10,
		R.drawable.img_circle_09, R.drawable.img_circle_08, R.drawable.img_circle_07,
		R.drawable.img_circle_06, R.drawable.img_circle_05, R.drawable.img_circle_04,
		R.drawable.img_circle_03, R.drawable.img_circle_02, R.drawable.img_circle_01,
	};

	private Paint mPaintBitmap = new Paint();

	private Bitmap mBackground = null;
	private Rect mRectBackground = new Rect();
	private Bitmap mCircle = null;
	private Rect mRectCircle = new Rect();

	private Paint mPaintSliderOut = new Paint();
	private Paint mPaintSliderOut2 = new Paint();
	private Path mPathSliderOut = new Path();
	private Path mPathSliderOut2 = new Path();
	private RectF mRectSliderOut = new RectF();
	private Bitmap mSliderOut = null;
	private PointF mPointSliderOutCenter = new PointF();
	private PointF mPointSliderOutMin = new PointF();
	private PointF mPointSliderOutMax = new PointF();

	private Paint mPaintSliderIn = new Paint();
	private Paint mPaintSliderIn2 = new Paint();
	private Path mPathSliderIn = new Path();
	private Path mPathSliderIn2 = new Path();
	private RectF mRectSliderIn = new RectF();
	private Bitmap mSliderIn = null;
	private PointF mPointSliderInCenter = new PointF();
	private PointF mPointSliderInCur = new PointF();

	private Paint mPaintCount = new Paint();
	private PointF mPointCount = new PointF();

	private Paint mPaintGoalCount = new Paint();
	private PointF mPointGoalCount = new PointF();

	private int mCircleStep = 0;

	private int mCurrentCount = 0;
	private int mSetCount = 0;


	public CircleView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		init();
	}

	public CircleView(Context context, AttributeSet attrs) {
		super(context, attrs);

		init();
	}

	public CircleView(Context context) {
		super(context);

		init();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		//super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		setMeasuredDimension(DEF_WIDTH, DEF_HEIGHT);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		//super.onDraw(canvas);

		if (null != mBackground) {
			canvas.drawBitmap(mBackground, mRectBackground.left, mRectBackground.top, mPaintBitmap);
		};
		if (null != mCircle) {
			canvas.drawBitmap(mCircle, mRectCircle.left, mRectCircle.top, mPaintBitmap);
		};

		canvas.drawPath(mPathSliderOut2, mPaintSliderOut2);
		canvas.drawPath(mPathSliderOut, mPaintSliderOut);
		if (null != mSliderOut) {
			canvas.drawBitmap(mSliderOut, mPointSliderOutMin.x, mPointSliderOutMin.y, mPaintBitmap);
			canvas.drawBitmap(mSliderOut, mPointSliderOutMax.x, mPointSliderOutMax.y, mPaintBitmap);
		}

		canvas.drawPath(mPathSliderIn2, mPaintSliderIn2);
		canvas.drawPath(mPathSliderIn, mPaintSliderIn);
		if (null != mSliderIn) {
			canvas.drawBitmap(mSliderIn, mPointSliderInCur.x, mPointSliderInCur.y, mPaintBitmap);
		}

		canvas.drawText(String.format("%02d", mCurrentCount), mPointCount.x, mPointCount.y, mPaintCount);
		if (-1 < mSetCount) {
			canvas.drawText(String.format("%02d", mSetCount), mPointGoalCount.x , mPointGoalCount.y, mPaintGoalCount);
		}
	}


	public void setSliderOut(float min, float max) {
		mPathSliderOut.reset();

		float start = (SLIDER_OUT_RANGE * min) / 100;
		float range = (SLIDER_OUT_RANGE * max) / 100 - start;
		start += SLIDER_OUT_START;
		mPathSliderOut.addArc(mRectSliderOut, start+4, range-8);

		float radius = mRectSliderOut.width() / 2;
		mPointSliderOutMin.x = mPointSliderOutCenter.x + (radius * (float)Math.cos(Math.toRadians(start))) - (mSliderOut.getWidth() / 2);
		mPointSliderOutMin.y = mPointSliderOutCenter.y + (radius * (float)Math.sin(Math.toRadians(start))) - (mSliderOut.getHeight() / 2);

		start += range;
		mPointSliderOutMax.x = mPointSliderOutCenter.x + (radius * (float)Math.cos(Math.toRadians(start))) - (mSliderOut.getWidth() / 2);
		mPointSliderOutMax.y = mPointSliderOutCenter.y + (radius * (float)Math.sin(Math.toRadians(start))) - (mSliderOut.getHeight() / 2);
	}

	public void setSliderIn(float val) {
		float range = (SLIDER_IN_RANGE * val) / 100;
		mPathSliderIn.reset();
		if (0 < val) {
			mPathSliderIn.addArc(mRectSliderIn, SLIDER_IN_START, range-7);
		}

		float radius = mRectSliderIn.width() / 2;
		range += SLIDER_IN_START;
		mPointSliderInCur.x = mPointSliderInCenter.x + (radius * (float)Math.cos(Math.toRadians(range))) - (mSliderIn.getWidth() / 2);
		mPointSliderInCur.y = mPointSliderInCenter.y + (radius * (float)Math.sin(Math.toRadians(range))) - (mSliderIn.getHeight() / 2);

		invalidate();
	}

	public void setCount(int cur, int set) {
		mCurrentCount = cur;
		if (mSetCount != set) {
			mSetCount = set;
			if (-1 < mSetCount) {
				mPointCount.y = mRectCircle.top + (mRectCircle.height() / 2) + 36;
			} else {
				mPointCount.y = mRectCircle.top + (mRectCircle.height() / 2) + 50;
			}
		}

		invalidate();
	}

	public void animation() {
		mCircleStep++;
		mCircle.recycle();
		mCircle = BitmapFactory.decodeResource(getResources(), IMAGE_CIRCLE[mCircleStep % IMAGE_CIRCLE.length]);

		invalidate();
	}


	private void init() {
		mBackground = BitmapFactory.decodeResource(getResources(), R.drawable.img_circle_bg);
		mRectBackground.left = (DEF_WIDTH - mBackground.getWidth()) / 2;
		mRectBackground.top = (DEF_HEIGHT - mBackground.getHeight());
		mRectBackground.right = mRectBackground.left + mBackground.getWidth();
		mRectBackground.bottom = mRectBackground.top + mBackground.getHeight();

		mCircle = BitmapFactory.decodeResource(getResources(), IMAGE_CIRCLE[0]);
		mRectCircle.left = (DEF_WIDTH - mCircle.getWidth()) / 2;
		mRectCircle.top = mRectBackground.top;//(DEF_HEIGHT - mBackground.getHeight());
		mRectCircle.right = mRectCircle.left + mCircle.getWidth();
		mRectCircle.bottom = mRectCircle.top + mCircle.getHeight();

		mPaintSliderOut.setAntiAlias(true);
		mPaintSliderOut.setColor(Color.WHITE);
		mPaintSliderOut.setStyle(Paint.Style.STROKE);
		mPaintSliderOut.setStrokeCap(Paint.Cap.ROUND);
		mPaintSliderOut.setStrokeWidth(10);
		mPaintSliderOut2.set(mPaintSliderOut);
		mPaintSliderOut2.setColor(0x7F7F7F7F);
		mRectSliderOut.set(mRectCircle.left-60, mRectCircle.top-60, mRectCircle.right+60, mRectCircle.bottom+60);
		mPathSliderOut2.addArc(mRectSliderOut, SLIDER_OUT_START, SLIDER_OUT_RANGE);
		mSliderOut = BitmapFactory.decodeResource(getResources(), R.drawable.img_circle_slider_small);
		mPointSliderOutCenter.x = mRectSliderOut.left + (mRectSliderOut.width() / 2);
		mPointSliderOutCenter.y = mRectSliderOut.top + (mRectSliderOut.height() / 2);

		mPaintSliderIn.setAntiAlias(true);
		mPaintSliderIn.setColor(Color.WHITE);
		mPaintSliderIn.setStyle(Paint.Style.STROKE);
		mPaintSliderIn.setStrokeCap(Paint.Cap.ROUND);
		mPaintSliderIn.setStrokeWidth(15);
		mPaintSliderIn2.set(mPaintSliderIn);
		mPaintSliderIn2.setColor(0x7F7F7F7F);
		mRectSliderIn.set(mRectCircle.left-30, mRectCircle.top-30, mRectCircle.right+30, mRectCircle.bottom+30);
		mPathSliderIn2.addArc(mRectSliderIn, SLIDER_IN_START, SLIDER_IN_RANGE);
		mSliderIn = BitmapFactory.decodeResource(getResources(), R.drawable.img_circle_slider_big);
		mPointSliderInCenter.x = mRectSliderIn.left + (mRectSliderIn.width() / 2);
		mPointSliderInCenter.y = mRectSliderIn.top + (mRectSliderIn.height() / 2);

		mPaintCount.setAntiAlias(true);
		mPaintCount.setColor(0xFFFFD200);
		mPaintCount.setTextAlign(Paint.Align.CENTER);
		mPaintCount.setTextSize(144);
		mPointCount.x = mRectCircle.left + (mRectCircle.width() / 2);
		mPointCount.y = mRectCircle.top + (mRectCircle.height() / 2) + 36;

		mPaintGoalCount.setAntiAlias(true);
		mPaintGoalCount.setColor(0xFFFFFFFF);
		mPaintGoalCount.setTextAlign(Paint.Align.CENTER);
		mPaintGoalCount.setTextSize(48);
		mPointGoalCount.x = mPointCount.x;
		mPointGoalCount.y = mPointCount.y + 68;
	}
}
